﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace iopt.solvency.pillarone.client.desktop.Views
{
    /// <summary>
    /// Logique d'interaction pour ActionDesigner.xaml
    /// </summary>
    public partial class ActionDesigner : UserControl
    {
        public ActionDesigner()
        {
            InitializeComponent();
        }

        internal static ICSharpCode.CodeCompletion.CSharpCompletion completion;
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (completion == null)
            {
                completion = new ICSharpCode.CodeCompletion.CSharpCompletion(new iopt.solvency.pillarone.client.desktop.Helpers.ScriptProvider());
            }
            var filepath = this.DataContext as ViewModels.VMActionDesigner;
            this.CodeEditor.TextChanged -= CodeEditor_TextChanged;          
            this.CodeEditor.OpenFile(filepath.FilePath);
            this.CodeEditor.Completion = completion;
            this.CodeEditor.SyntaxHighlighting = ICSharpCode.AvalonEdit.Highlighting.HighlightingManager.Instance.GetDefinition("C#");
            this.CodeEditor.TextChanged += CodeEditor_TextChanged;
        }

        void CodeEditor_TextChanged(object sender, EventArgs e)
        {
            try
            {
                this.CodeEditor.SaveFile();
            }
            catch (Exception ex)
            {

            }
        }
    }
}
